package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Contacts;
import com.example.demo.mapper.ContactsMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author sean
 */
@RestController
@RequestMapping("/contacts")
public class ContactsController extends BaseController {

    @Resource
    ContactsMapper contactsMapper;

    @PostMapping
    public Result<?> save(@RequestBody Contacts contacts) {
        contacts.setCreateTime(new Date());
        return Result.success(contactsMapper.insert(contacts));
    }

    @PutMapping
    public Result<?> update(@RequestBody Contacts contacts) {
        return Result.success(contactsMapper.updateById(contacts));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return Result.success(contactsMapper.deleteById(id));
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(contactsMapper.selectById(id));
    }

    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(contactsMapper.selectList(null));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Contacts> page = contactsMapper.findPage(new Page<>(pageNum, pageSize));
        return Result.success(page);
    }

}
